package nero.yncc.com.createart.utils;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.custom.MyRecyclerView;

/**
 * Created by nero on 17-6-26.
 */

public class LIstPopUtil2 {

    ArrayList<String> arrayList;

    MyRecyclerView rc_list;
    SingleTextAdapter adapter;

    Context context;

    PopupWindow popupWindow;
    View view;

    public LIstPopUtil2(Context context, ArrayList<String> arrayList) {
        this.arrayList = arrayList;
        this.context = context;


        view = LinearLayout.inflate(context, R.layout.rc_single_pop2, null);
        view.setLayoutParams(new LinearLayout.LayoutParams(-1,-1));
        rc_list = view.findViewById(R.id.rc_list);
        adapter = new SingleTextAdapter(arrayList, context);
        rc_list.setAdapter(adapter);
        MyLayoutManager manager = new MyLayoutManager(context);
        rc_list.setLayoutManager(manager);

    }

    class SingleTextAdapter extends RecyclerView.Adapter<SingleTextAdapter.Holder> {
        ArrayList<String> arrayList;
        Context context;

        public SingleTextAdapter(ArrayList<String> arrayList, Context context) {
            this.arrayList = arrayList;
            this.context = context;
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LinearLayout.inflate(context, R.layout.single_textview_pop2, null);
            Holder holder = new Holder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            holder.textView.setText(arrayList.get(position));
        }

        @Override
        public int getItemCount() {
            return arrayList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            public Holder(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.tv_content);
            }

            TextView textView;
        }
    }

    public void flush() {
        adapter.notifyDataSetChanged();
        adapter.notifyItemChanged(1);
    }

    public void show(View views, PopupWindow.OnDismissListener d) {
        if (popupWindow == null){
            popupWindow = new PopupWindow(view, -1, -1);
            popupWindow.setBackgroundDrawable(new ColorDrawable(0));
            popupWindow.setOutsideTouchable(true);
            popupWindow.setOnDismissListener(d);
        }

        popupWindow.showAsDropDown(views,0,3);
    }

    public void dismiss() {
        if (popupWindow != null)
            popupWindow.dismiss();
    }

    public boolean isShowing(){
        if(popupWindow==null)
            return false;
        return popupWindow.isShowing();
    }

}
